package com.ui.basic.controller.trans;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.trans.CommandCashtrans;
import com.ui.basic.ibatis.model.system.TblSUser;
import com.ui.basic.service.trans.CashTransService;

@Controller
public class CashTransController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
		.getLog(CashTransController.class);
	
	@Autowired
	private CashTransService cashTransService;

	public void setCashTransService(CashTransService cashTransService) {
		this.cashTransService = cashTransService;
	}
	
	@RequestMapping(value = "/trans/cashTrans.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>>CashTransController.page");
		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "cashTrans.page";
	}

	@RequestMapping(value = "/trans/cashTrans.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCashtrans") CommandCashtrans commandCashtrans) {
		LOGGER.debug(">>>CashTransController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}
		
		TblSUser user = (TblSUser) request.getSession().getAttribute(Globals.USER_KEY);
		if(user != null){
			commandCashtrans.setLocId(user.getLocId());
		}
		
		try {
			cashTransService.setCombo(commandCashtrans);
			model.addAttribute("commandCashtrans", cashTransService.getCashTrans(commandCashtrans));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "cashTrans.view";
	}

	@RequestMapping(value = "/trans/cashTrans.form", method = RequestMethod.POST)
	public String showForm(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCashtrans") CommandCashtrans commandCashtrans) {
		LOGGER.debug(">>>CashTransController.showForm");
		return edit(model, request, response, commandCashtrans);
	}
	
	@RequestMapping(value = "/trans/cashTrans.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCashtrans") CommandCashtrans commandCashtrans) {
		LOGGER.debug(">>>CashTransController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		TblSUser user = (TblSUser) request.getSession().getAttribute(Globals.USER_KEY);
		if(user != null){
			commandCashtrans.setLocId(user.getLocId());
		}

		try {
			cashTransService.setCombo(commandCashtrans);
			if (commandCashtrans.getId() != null)
				model.addAttribute("commandCashtrans", cashTransService
						.getCashTransDetail(commandCashtrans));
			else {
				model.addAttribute("commandCashtrans", commandCashtrans);
				commandCashtrans.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "cashTrans.edit";
	}
}
